﻿
module Sustainalytics.EsgRatings {

    import Common = Sustainalytics.Common;
    import EsgRatingsAdmin = Sustainalytics.EsgRatings.Admin;

    export class DownloadReportController {

        public weightMatrices: EsgRatingsAdmin.WeightMatrix[] = [];
        public selectedWeightMatrix: EsgRatingsAdmin.WeightMatrix = null;

        public referenceUniverses: EsgRatingsAdmin.IReferenceUniverse[] = [];
        public selectedReferenceUniverse: EsgRatingsAdmin.IReferenceUniverse = null;

        public comparativeGroups: { name: string; value: number; }[];
        public selectedComparativeGroup: { name: string; value: number; };

        public companyTypes: { name: string; value: number; }[];
        public selectedCompanyType: { name: string; value: number; };

        public templateTypes: { name: string; value: number; }[];
        public selectedTemplateType: { name: string; value: number; };

        private userId: string;
        private accountId: string;
        private accountTier: number;
        private esgProfileId: string;


        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private WeightMatricesService: EsgRatingsAdmin.IWeightMatricesService,
            private ReferenceUniversesService: EsgRatingsAdmin.IReferenceUniversesService,
            private notificationFactory: Common.INotificationFactory,
            private $modal: Common.IModal,
            private $modalInstance: Common.IModalInstance,
            private companyId: string,
            private companyName: string) {

            var ctx = this;
            $scope['downloadReportController'] = ctx;



            ctx.comparativeGroups =
            [
                { name: "Peer Group", value: ComparativeGroupEnum.PeerGroup },
                { name: "Industry Group", value: ComparativeGroupEnum.IndustryGroup }
            ];
            ctx.selectedComparativeGroup = ctx.comparativeGroups[0];

            ctx.companyTypes =
                [
                { name: "No", value: 1 },
                { name: "Yes", value: CompanyTypeEnum.Combined }
            ];

            ctx.selectedCompanyType = ctx.companyTypes[1];

            ctx.templateTypes =
            [
                { name: "No", value: 1 },
                { name: "Yes", value: 2 }
            ];

            ctx.selectedTemplateType = ctx.templateTypes[1];


            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.userId = ctx.$rootScope['userInfo'].userId;

            ctx.accountId = ctx.$rootScope['userInfo'].accountId;
            ctx.accountTier = ctx.$rootScope['userInfo'].accountTier;
            ctx.esgProfileId = ctx.$rootScope['userInfo'].esgProfileId;

            ctx.LoadWeightMatrixes();

            ctx.ReferenceUniversesService.getReferenceUniverses()
                .then(data => {
                    ctx.referenceUniverses = data;
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].name == 'Default') {
                                ctx.selectedReferenceUniverse = data[i];
                            }
                        }
                    }
                });
        }

        public LoadWeightMatrixes(): ng.IPromise<any> {
            var ctx = this;

            ctx.weightMatrices = [];
            ctx.selectedWeightMatrix = null;

            var def = ctx.$q.defer();

            var defaultMatrix = {
                id: "00000000-0000-0000-0000-000000000000",
                name: "Default Weight Matrix",
                accountId: "00000000-0000-0000-0000-000000000000",
                isSelectedInActiveProfile: false,
                markedForDeletionOn: null,
                processedDeletionOn: null
            };

            ctx.weightMatrices.push(defaultMatrix);
            ctx.selectedWeightMatrix = defaultMatrix;

            var weightmatrices = ctx.WeightMatricesService.listWeightMatricesByAccountId(ctx.accountId, false);

            weightmatrices.then(data => {
                for (var i = 0; i < data.length; i++) {
                    ctx.weightMatrices.push(data[i]);
                }

            }, (errorReason => { console.log(errorReason); }));

            return def.promise;;
        }

        public selectMatrix() {
            var ctx = this;

        }

        public cancel = function () {
            this.$modalInstance.dismiss('cancel');
        };


        public isValidParameters() {

            var isNotValid = true;

            if (this.selectedWeightMatrix && this.selectedReferenceUniverse) {
                isNotValid = false;
            }

            return isNotValid;
        }

        public downloadReport = function () {

            var parameters = new EsgCompanyReportParametersDto();
            parameters.accountId = this.accountId;
            parameters.accountTier = this.accountTier;
            parameters.esgProfileId = this.esgProfileId;
            parameters.companyId = this.companyId;

            parameters.matrixId = this.selectedWeightMatrix.id;
            parameters.referenceUniverseId = this.selectedReferenceUniverse.id;
            parameters.comparativeGroup = this.selectedComparativeGroup.value;
            parameters.companyType = this.selectedCompanyType.value;
            parameters.templateType = this.selectedTemplateType.value;

            //Added for google analytics
            parameters.matrixName = this.selectedWeightMatrix.name;
            parameters.referenceUniverseName = this.selectedReferenceUniverse.name;
            parameters.comparativeGroupName = this.selectedComparativeGroup.name;

            this.$modalInstance.close(parameters);
        }
    }

    angular.module(esgRatingsModuleName).controller('DownloadReportController',
        ['$rootScope', '$scope', '$q', '$location', 'WeightMatricesService', 'ReferenceUniversesService', 'notificationFactory', '$modal', '$modalInstance', 'companyId', 'companyName', DownloadReportController]);
} 
